package com.chaber.containers.views;

import javax.annotation.PostConstruct;

import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

public abstract class AbstractAddModRemView extends Stage {

	protected Scene scene;
	protected BorderPane contentPane;
	protected VBox errorsBox;

	// action buttons
	protected Button saveButton;
	protected Button removeButton;
	protected Button initModifyButton;

	public AbstractAddModRemView(String title) {
		this.setTitle(title);

		this.contentPane = new BorderPane();
		
		this.errorsBox = this.buildErrorsBox();
		BorderPane.setAlignment(errorsBox, Pos.CENTER);
		this.contentPane.setTop(this.errorsBox);

		Node detailsFields = this.buildDetailsFields();
		BorderPane.setAlignment(detailsFields, Pos.CENTER);
		this.contentPane.setCenter(detailsFields);

		HBox actionButtonsBox = this.buildActionButtons();
		BorderPane.setAlignment(actionButtonsBox, Pos.TOP_CENTER);
		this.contentPane.setBottom(actionButtonsBox);

		this.scene = new Scene(this.contentPane, 500, 500);
		this.setScene(this.scene);
		this.initModality(Modality.APPLICATION_MODAL);

		// clear the error messages
		this.setOnCloseRequest(new EventHandler<WindowEvent>() {
			@Override
			public void handle(WindowEvent arg0) {
				errorsBox.getChildren().clear();
			}
		});
	}
	
	@PostConstruct
	public void postConstruct() {
		this.deactivateEdition();
	}

	protected abstract Node buildDetailsFields();

	public abstract void activateEdition();

	public abstract void deactivateEdition();

	public abstract void clearFields();

	protected VBox buildErrorsBox() {
		VBox errorsBox = new VBox();

		errorsBox.setVisible(true);
		errorsBox.setAlignment(Pos.CENTER);
		errorsBox.setPadding(new Insets(10));
		errorsBox.setSpacing(10);

		return errorsBox;
	}

	protected HBox buildActionButtons() {
		HBox actionButtonsBox = new HBox();

		this.saveButton = new Button("Save");
		actionButtonsBox.getChildren().add(this.saveButton);

		this.removeButton = new Button("Remove");
		actionButtonsBox.getChildren().add(this.removeButton);

		this.initModifyButton = new Button("Modify");
		actionButtonsBox.getChildren().add(this.initModifyButton);

		actionButtonsBox.setSpacing(15);
		actionButtonsBox.setPadding(new Insets(50));
		actionButtonsBox.setAlignment(Pos.TOP_CENTER);

		return actionButtonsBox;
	}

	public BorderPane getContentPane() {
		return contentPane;
	}

	public void setContentPane(BorderPane contentPane) {
		this.contentPane = contentPane;
	}

	public VBox getErrorsBox() {
		return errorsBox;
	}

	public void setErrorsBox(VBox errorsBox) {
		this.errorsBox = errorsBox;
	}

	public Button getSaveButton() {
		return saveButton;
	}

	public void setSaveButton(Button saveButton) {
		this.saveButton = saveButton;
	}

	public Button getRemoveButton() {
		return removeButton;
	}

	public void setRemoveButton(Button removeButton) {
		this.removeButton = removeButton;
	}

	public Button getInitModifyButton() {
		return initModifyButton;
	}

	public void setInitModifyButton(Button initModifyButton) {
		this.initModifyButton = initModifyButton;
	}
}
